﻿
    using System;
    using System.Windows;
    using System.Windows.Media;
namespace WCYCReader.Utility
{
    public class FindFirstElement
    {
        public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T: DependencyObject
        {
            int childrenCount = VisualTreeHelper.GetChildrenCount(parentElement);
            if (childrenCount != 0)
            {
                for (int i = 0; i < childrenCount; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(parentElement, i);
                    if ((child != null) && (child is T))
                    {
                        return (T) child;
                    }
                    T local = FindFirstElementInVisualTree<T>(child);
                    if (local != null)
                    {
                        return local;
                    }
                }
            }
            return default(T);
        }
    }
}

